package org.makcro.sms.services.chat.base.business;

import java.text.MessageFormat;
import java.util.Hashtable;
import java.util.List;

import org.apache.log4j.Logger;
import org.makcro.sms.services.chat.base.service.ChatService;

import com.klikmee.sms.cef.bulk.BulkSender;
import com.klikmee.sms.cef.bulk.SendBulkException;
import com.klikmee.sms.cef.bulk.BulkSender.BulkParams;

/**
 * Service used to send bulk messages
 * 
 * @author makcro
 * 
 */
public class BulkService {

	/**
	 * Logger instance
	 */
	private Logger		logger	= Logger.getLogger( BulkService.class );

	/**
	 * Instance of bulk sender
	 */
	private BulkSender	bulkSender;
	

	/**
	 * Sends bulk message by delegating it to the instance of
	 * {@link ChatService}
	 * 
	 * @param messageText
	 * @param signature
	 * @param receivers
	 * @return
	 */
	public int sendBulk(String messageText, String signature, List<String> receivers) {
		// create bulk params
		Hashtable<BulkParams, Object> params = new Hashtable<BulkSender.BulkParams, Object>();
		params.put( BulkParams.TEXT, messageText );
		params.put( BulkParams.SENDER, signature );
		params.put( BulkParams.RECEIVERS, receivers );
		
		try {
			int messagesSent = bulkSender.send( params );
			logger.info( MessageFormat.format( "[{0}] bulk message(s) forwarded to bulk sender ", messagesSent ) );
			return messagesSent;
		}
		catch (SendBulkException e) {
			logger.fatal( "Error sending bulk message(s)", e );
		}
		return -1;
	}

	public void setBulkSender(BulkSender bulkSender) {
		this.bulkSender = bulkSender;
	}
	
	
}
